/*
 * Angry Monsters, Windows Mobile 7 XNA Game FW demo
 * Tero Paananen, tepaanan@gmail.com
 * 2011 Finland
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input.Touch;

// http://box2dxna.codeplex.com
using Box2D.XNA;

namespace WP7Box2d
{
    class LevelPreview : LevelHandler
    {
        public event GameStateChangedEventHandler GameStateChangedFromPreview;

        private int m_levelIndexToShow = 0;

        public LevelPreview(ContentManager cm, ref BoxHandler box)
        {
            m_cm = cm;
            m_boxHandler = box;
        }

        public void UpdateLevelIndex()
        {
            m_levelIndexToShow = m_levelStorage.LatestLevelIndex();
            if (m_levelIndexToShow < 0)
                m_levelIndexToShow = 0;
        }

        public override void CreateLevelStorage(bool createDefaultLevels)
        {
            m_levelStorage = new LevelStorage(createDefaultLevels);
            UpdateLevelIndex();
        }

        public void LoadAndShowLevels()
        {
            if (m_levelStorage.LatestLevelIndex() == 0)
                return;

            if (m_levelIndexToShow > m_levelStorage.LatestLevelIndex() - 1)
                m_levelIndexToShow = 0;

            if (m_levelIndexToShow < 0)
                m_levelIndexToShow = m_levelStorage.LatestLevelIndex() - 1;

            LoadNextLevel();            
        }

        /// <summary>
        /// Load next level from isolated storage and create level items
        /// </summary>
        private void LoadNextLevel()
        {
            m_levelData.Clear();
            int levelIndex = m_levelIndexToShow + 1;
            System.Diagnostics.Debug.WriteLine(levelIndex);

            String levelFileName = LevelStorage.LEVELNAME;
            levelFileName += levelIndex.ToString();
            System.Diagnostics.Debug.WriteLine("LoadNextLevel name " + levelFileName);
            List<LevelData> levelData = m_levelStorage.LoadLevel(levelFileName);
            if (levelData != null)
            {
                System.Diagnostics.Debug.WriteLine("Level founds");
                m_levelIndexToShow++;
                for (int i = 0; i < levelData.Count(); i++)
                {
                    LevelData data = levelData[i];
                    LevelEditorItem item = CreateLevelItem(data);
                    m_levelData.Add(item);
                }
            }
            else
            {
                if (!m_levelStorage.LevelExists())
                {
                    System.Windows.MessageBox.Show("CREATE level first");
                }
            }
        }

        /// <summary>
        /// CreateLevelEditorItem from LevelData
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public LevelEditorItem CreateLevelItem(LevelData data)
        {
            LevelEditorItem item = new LevelEditorItem(false,
                (LevelEditorItem.EditorItemType)data.m_type,
                data.m_rectangle,
                m_boxHandler.GetLevelItemTexture((LevelEditorItem.EditorItemType)data.m_type));

            return item;
        }

        public override void LoadContent()
        {
            LoadContent(m_cm);
        }

        protected override void LoadContent(ContentManager cm)
        {
            base.LoadContent(cm);
        }

        public override void ButtonPressed(Button.ButtonType buttonType)
        {
            System.Diagnostics.Debug.WriteLine("LevelPreview " + buttonType);
            switch (buttonType)
            {
                case Button.ButtonType.ELeftBtn:
                    {
                        m_levelIndexToShow--;
                        m_levelIndexToShow--;
                        LoadAndShowLevels();
                        break;
                    }
                case Button.ButtonType.ERightBtn:
                    {
                        LoadAndShowLevels();
                        break;
                    }
                case Button.ButtonType.EPlayBtn:
                    {
                        if (m_levelData.Count() > 0)
                        {
                            CreateBoxModelFromTextures();
                            m_boxHandler.m_catapultPos.X = m_boxHandler.m_catapult.Width;
                            m_cameraOffset = 0.0f;
                            GameStateChangedFromPreview(GameEngine.GameState.EDemoScreensState);
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("CREATE level first!");
                        }
                        break;
                    }
                case Button.ButtonType.ECancelBtn:
                    {
                        Clear();
                        GameStateChangedFromPreview(GameEngine.GameState.EMenuScreenState);
                        break;
                    }
            }
        }


        public override void Update(GameTime gameTime, float cameraOffset)
        {
            Vector2 touchPosition;
            TouchCollection touchCollection = TouchPanel.GetState();
            if (touchCollection.Count() > 0)
            {
                TouchLocation tl = touchCollection.First();
                touchPosition = tl.Position;

                if (tl.State == TouchLocationState.Pressed)
                {
                    if (tl.State == TouchLocationState.Pressed)
                    {
                        m_playBtn.handleTouch(touchPosition);
                        m_leftBtn.handleTouch(touchPosition);
                        m_rightBtn.handleTouch(touchPosition);
                        m_cancelBtn.handleTouch(touchPosition);
                    }
                }
            }
        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            m_playBtn.Draw(spriteBatch, gameTime);
            m_cancelBtn.Draw(spriteBatch, gameTime);
            m_leftBtn.Draw(spriteBatch, gameTime);
            m_rightBtn.Draw(spriteBatch, gameTime);

            for (int i = 0; i < m_levelData.Count(); i++)
            {
                LevelEditorItem item = m_levelData[i];
                spriteBatch.Draw(item.m_texture, item.m_rectangle, Color.White);
            }
        }



    }
}
